<?php

class Controller_Media extends Controller_Abstract
{
	function actionImpression()
	{
		$camid = $this->_context->cam;
		if (empty($camid))
		{
			header(Util::HTTPStatus(404));
			exit;
		}
		$media = new Media();
		$campaign = $media->getCampaignById($camid);
		if ($campaign == false)
		{
			header(Util::HTTPStatus(404));
			exit;
		}
		$creatives = $media->getCreativesByOfferId($campaign['o_id']);
		if (count($creatives)<1)
		{
			header(Util::HTTPStatus(404));
			exit;
		}
		$rand = rand(0, count($creatives)-1);
		setcookie(Q::ini('creative_cookie_key'), $creatives[$rand]['id'], time()+60*60*24*365*10, '/', COOKIE_DOMAIN, 0, 1);
		$hitid = $media->getHitId();
		$hit = array();
		$hit['session'] = session_id();
		$hit['event'] = 'impression';
		$hit['time'] = time();
		$hit['ip'] = $_SERVER['REMOTE_ADDR'];
		$hit['cam'] = 'cgb-'.$camid;
		$hit['offer'] = $campaign['o_id'];
		$hit['cre'] = $creatives[$rand]['id'];
		$hit['pub'] = $campaign['pub_id'];
		$hit['sub'] = $campaign['sub_id'];
		$media->addHit("hit:".$hitid, $hit);
//		$media->addToSession($hit['session'], $hit['time'], $hitid);
//		$media->saveSession($hit['session']);
		$media->addToTimeframe($hit['time'], $hitid);
		header("Location: ".$creatives[$rand]['url']);
		exit;
	}
	function actionClick()
	{	
		$creativeId = $_COOKIE[Q::ini('creative_cookie_key')];
		if(empty($creativeId))
		{
			$creativeId = 0;
		}
		$cus = $this->_context->cus;
		if(!empty($cus)){
			$_SESSION['cus'] = $cus;
		}
		
		$camid = $this->_context->cam;
		if (empty($camid))
		{
			header(Util::HTTPStatus(404));
			exit;
		}
		$media = new Media();
		$campaign = $media->getCampaignById($camid);
		if ($campaign == false)
		{
			header(Util::HTTPStatus(404));
			exit;
		}
		$cookie = $_COOKIE[Q::ini('landing_cookie_key')];
		$cookie_window = false;
		$ip_window = false;
		if (empty($cookie))
		{
			$urls = $media->getLandingUrlsByOfferId($campaign['o_id']);
			if (count($urls)<1)
			{
				header(Util::HTTPStatus(404));
				exit;
			}
			$url = $this->getUrlByRandom($urls);
			setcookie(Q::ini('landing_cookie_key'), $url['id'], time()+60*60*24*$campaign['cookie_window'], '/', '' . COOKIE_DOMAIN, 0, 1);
			$updated = $media->getIpLog($_SERVER['REMOTE_ADDR'], $campaign['id']);
			if ($updated == false)
			{
				$media->addIpLog($_SERVER['REMOTE_ADDR'], $campaign['id']);
			}
			else if ((time()-$updated) < $campaign['ip_window']*60*60*24)
			{
				$ip_window = true;
			}
			else
			{
				$media->updateIpLog($ip);
			}
		}
		else
		{
			$url = $media->getLandingUrlById($cookie);
			if (!empty($cookie))
			{
				$cookie_window = true;
			}
		}
		$hitid = $media->getHitId();
		$hit = array();
		$hit['session'] = session_id();
		$hit['event'] = 'click';
		$hit['time'] = time();
		$hit['ip'] = $_SERVER['REMOTE_ADDR'];
		$hit['cam'] = 'cgb-'.$camid;
		$hit['cre'] = $creativeId;
		$hit['cookie_window'] = $cookie_window;
		$hit['ip_window'] = $ip_window;
		$media->addHit("hit:".$hitid, $hit);
//		$media->addToSession($hit['session'], $hit['time'], $hitid);
//		$media->saveSession($hit['session']);
		$media->addToTimeframe($hit['time'], $hitid);
		header("Location: " . $url['url']);
		exit;
	}
	function actionPixel()
	{
		$camid = $this->_context->cam;
		if (empty($camid))
		{
			header(Util::HTTPStatus(404));
			exit;
		}
		$media = new Media();
		$campaign = $media->getCampaignById($camid);
		if ($campaign == false)
		{
			header(Util::HTTPStatus(404));
			exit;
		}
		$event = $this->_context->evt;
		if (empty($event))
		{
			header(Util::HTTPStatus(404));
			exit;
		}
		$events = array ( 
			0 => 'landing',
			1 => 'basic_reg',
			2 => 'full_reg',
			3 => 'download',
			4 => 'installation',
			5 => 'conv1',
			6 => 'conv2',
			7 => 'conv3',
			8 => 'conv4',
			9 => 'conv5',
			);
		$convpos = $media->getConvPosByOffer($campaign['o_id']);
		$ip_window = false;
		$cookie_window = false;
		if ($events[$convpos] == $event)
		{
			$cookie = $_COOKIE[Q::ini('conversion_cookie_key')];
			$iplog = $media->getIpLog($_SERVER['REMOTE_ADDR'], $camid);
			if (empty($cookie) && ($iplog == false || (time()-$iplog) > 60*60*24*$campaign['ip_window']))
			{
				setcookie(Q::ini('conversion_cookie_key'), 1, time()+60*60*24*$campaign['cookie_window'], '/', '' . COOKIE_DOMAIN, 0, 1);
				$media->addIpLog($_SERVER['REMOTE_ADDR'], $camid);
			}
			else if (!$empty($cookie))
			{
				$cookie_window = true;
			}
			else
			{
				$ip_window = true;
			}
		}
		$hitid = $media->getHitId();
		$hit = array();
		$hit['session'] = session_id();
		$hit['event'] = $event;
		$hit['time'] = time();
		$hit['ip'] = $_SERVER['REMOTE_ADDR'];
		$hit['cam'] = 'cgb-'.$camid;
		$hit['cookie_window'] = $cookie_window;
		$hit['ip_window'] = $ip_window;
		$media->addHit("hit:".$hitid, $hit);
//		$media->addToSession($hit['session'], $hit['time'], $hitid);
//		$media->saveSession($hit['session']);
		$media->addToTimeframe($hit['time'], $hitid);
		exit;
	}
	private function getUrlByRandom($urls)
	{
		$total = 0;
		$fromto = array();
		foreach($urls as $url)
		{
			$fromto[] = array('from' => $total, 'to' => $total+$url['weight'], 'url' => $url);
			$total += $url['weight'];
		}
		print_r($total);
		$rand = rand(0, $total-1);
		print_r($rand);
		foreach ($fromto as $value)
		{
			if ($rand >= $value['from'] && $rand < $value['to'])
			{
				return $value['url'];
			}
		}
	}
}
?>